package com.jhy.study2.IO;


import java.io.*;

/**
 * @author: robert
 * @description: 文件书写
 * @date: 2021/8/23 18:14
 * @return
 */
public class TxtIOWriteAndReadClass1 {

    public static void main(String[] args) {
//        writeText();
        readText();






    }


    public static void writeText(){
        File f = new File("D://testIO");
        if(!f.exists()){
            f.mkdirs();

        }

        File f2 = new File("D://testIO//newf.txt");
        if(f2.exists()){
            try {
                f2.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try(BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f2),"utf-8"))){
            bf.write("第一行文字abc");
            bf.newLine();
            bf.write("小黑sssssssssssss!!#$@#^%$%*&^*&^*");

        }catch(Exception e){
            e.printStackTrace();
        }

    }

    public static void readText(){
        File f = new File("D://testIO//newf.txt");
        try(BufferedReader bd = new BufferedReader(new InputStreamReader(new FileInputStream(f),"utf-8"))){
//            while(bd.read() != -1){  //这么会丢失每一行的第一个字符
//                System.out.println(bd.readLine());
//            }
            String line;
            while( (line = bd.readLine()) != null){
                System.out.println(line);
            }


        }catch(Exception e){
            e.printStackTrace();
        }

    }


}
